/// 使用特征对象特性在屏幕上绘制按钮与下拉选框

//创建绘制特征
pub trait Draw {
    fn draw(&self) -> String;
}
// 创建按钮组件
pub struct Button {
    pub width: u32,
    pub height: u32,
    pub label: String,
}
// 绘制Button方法
impl Draw for Button {
    fn draw(&self) -> String {
        // println!("label: {}, width: {}, height: {}", self.label, self.width, self.height);
        format!("label: {}, width: {}, height: {}", self.label, self.width, self.height)
    }
}

// 创建下拉选框
pub struct SelectBox {
    width: u32,
    height: u32,
    options: Vec<String>,
}

// 绘制selectbox方法
impl Draw for SelectBox {
    fn draw(&self) -> String {
        // println!("options: {:?}, width: {}, height: {}", self.options, self.width, self.height);
        format!("options: {:?}, width: {}, height: {}", self.options, self.width, self.height)
    }
}

// 创建屏幕
pub struct Screen {
    pub components: Vec<Box<dyn Draw>>, // 引入动态特征做为数组元素类型
}

// 创建屏幕方法
impl Screen {
    pub fn run(&self) {
        for component in self.components.iter() {
            println!("{:?}", component.draw());
            component.draw();
        }
    }
}




fn main() {
    let b1 = Button{width: 100u32, height: 80, label: "Submit".to_string()};
    let s1 = SelectBox{width: 100u32, height: 20, options: vec!["option1".to_string(), "option2".to_string()]};
    let scn = Screen{components: vec![Box::new(b1), Box::new(s1)]};
    scn.run()
}
